<?php

namespace App\Models\Wanggui;

use App\Models\BaseModel;
use App\Models\Common\MapNationalIndustrialParkModel;
use App\Models\Common\MapNationalStreetModel;
use App\Models\Common\MapNationalUniversitiesModel;
use App\Models\Common\MapTownshipPlanAreaModel;
use App\Models\Kms\User\KmsUser;
use App\Models\OpenShop\Common\SiteLocationMallModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class CustomerDepartmentTaskModel extends BaseModel
{
    use HasFactory;

    protected $table = 'customer_department_task';

    protected $fillable = [
        'wgNumber',
        'wgType',
        'wgName',
        'ranking',
        'assignId',
        'assignTime',
        'manageStatus',
        'projectPhase',
        'grade',
        'settle',
        'collect',
        'prefixAddress',
        'address',
        'provinceCode',
        'cityCode',
        'districtCode',
        'followUpTime',
        'preHaving',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
    ];

    /**
     * 网规类型
     */
    const WG_TYPE_MALL       = 0;
    const WG_TYPE_STREET     = 1;
    const WG_TYPE_UNIVERSITY = 2;
    const WG_TYPE_PARK       = 3;
    const WG_TYPE_TOWN       = 4;

    const WG_TYPE_STR_MAPPING = [
        self::WG_TYPE_MALL       => '商场',
        self::WG_TYPE_STREET     => '街道',
        self::WG_TYPE_UNIVERSITY => '高校',
        self::WG_TYPE_PARK       => '产业园',
        self::WG_TYPE_TOWN       => '乡镇',
    ];

    const WG_TYPE_MODEL_MAPPING = [
        self::WG_TYPE_MALL       => SiteLocationMallModel::class,
        self::WG_TYPE_STREET     => MapNationalStreetModel::class,
        self::WG_TYPE_UNIVERSITY => MapNationalUniversitiesModel::class,
        self::WG_TYPE_PARK       => MapNationalIndustrialParkModel::class,
        self::WG_TYPE_TOWN       => MapTownshipPlanAreaModel::class,
    ];


    /**
     * 网规等级
     */
    const GRADE_S = 6;
    const GRADE_A = 5;
    const GRADE_B = 4;
    const GRADE_C = 3;
    const GRADE_D = 2;

    const GRADE_MAPPING = [
        self::GRADE_S => 'S级',
        self::GRADE_A => 'A级',
        self::GRADE_B => 'B级',
        self::GRADE_C => 'C级',
        self::GRADE_D => 'D级',
    ];

    /**
     * 入驻状态
     */
    const SETTLE_STATUS_OK  = 1;
    const SETTLE_STATUS_OFF = 0;

    const SETTLE_STATUS_MAPPING = [
        self::SETTLE_STATUS_OFF => '未入驻',
        self::SETTLE_STATUS_OK  => '已入驻',
    ];

    /**
     * 是否收藏
     */
    const COLLECT_OK  = 1;
    const COLLECT_OFF = 0;

    const COLLECT_MAPPING = [
        self::COLLECT_OFF => '未收藏',
        self::COLLECT_OK  => '已收藏',
    ];

    /**
     * 管理状态
     */
    const MANAGE_STATUS_WAIT_ASSIGN = 0;
    const MANAGE_STATUS_ASSIGN      = 1;
    const MANAGE_STATUS_COMPLETE    = 2;

    const MANAGE_STATUS_MAPPING = [
        self::MANAGE_STATUS_WAIT_ASSIGN => '待指派',
        self::MANAGE_STATUS_ASSIGN      => '已指派',
        self::MANAGE_STATUS_COMPLETE    => '已完成',
    ];

    /**
     * 项目阶段
     */
    const PROJECT_PHASE_WAIT_FOLLOW_UP  = 1;
    const PROJECT_PHASE_NO_CLUE         = 2;
    const PROJECT_PHASE_EFFECTIVE_CLUE  = 3;
    const PROJECT_PHASE_EFFECTIVE_HOUSE = 4;
    const PROJECT_PHASE_SITE_LOCATION   = 5;
    const PROJECT_PHASE_SITE_CONFIRM    = 6;
    const PROJECT_PHASE_MAPPING         = [
        self::PROJECT_PHASE_WAIT_FOLLOW_UP  => '待跟进',
        self::PROJECT_PHASE_NO_CLUE         => '无线索',
        self::PROJECT_PHASE_EFFECTIVE_CLUE  => '有效线索',
        self::PROJECT_PHASE_EFFECTIVE_HOUSE => '有效房源',
        self::PROJECT_PHASE_SITE_LOCATION   => '已确址',
        self::PROJECT_PHASE_SITE_CONFIRM    => '已完成',
    ];

    /**
     * 排序方式
     */
    const SORT_TASK_COUNT_MORE_TO_LESS         = 1;
    const SORT_TASK_COUNT_LESS_TO_MORE         = 2;
    const SORT_S_A_COUNT_MORE_TO_LESS          = 3;
    const SORT_S_A_COUNT_LESS_TO_MORE          = 4;
    const SORT_S_A_RATIO_MORE_TO_LESS          = 5;
    const SORT_S_A_RATIO_LESS_TO_MORE          = 6;
    const SORT_ON_THE_JOB_TIME_FAR_TO_NEAR     = 7;
    const SORT_ON_THE_JOB_TIME_NEAR_TO_FAR     = 8;
    const SORT_HOUSE_COUNT_MORE_TO_LESS        = 9;
    const SORT_HOUSE_COUNT_LESS_TO_MORE        = 10;
    const SORT_SITE_CONFIRM_COUNT_MORE_TO_LESS = 11;
    const SORT_SITE_CONFIRM_COUNT_LESS_TO_MORE = 12;

    const SORT_MAPPING = [
        self::SORT_TASK_COUNT_MORE_TO_LESS         => '任务数多到少',
        self::SORT_TASK_COUNT_LESS_TO_MORE         => '任务数少到多',
        self::SORT_S_A_COUNT_MORE_TO_LESS          => 'SA数量多到少',
        self::SORT_S_A_COUNT_LESS_TO_MORE          => 'SA数量少到多',
        self::SORT_S_A_RATIO_MORE_TO_LESS          => 'SA占比多到少',
        self::SORT_S_A_RATIO_LESS_TO_MORE          => 'SA占比少到多',
        self::SORT_ON_THE_JOB_TIME_FAR_TO_NEAR     => '入职时间远到近',
        self::SORT_ON_THE_JOB_TIME_NEAR_TO_FAR     => '入职时间近到远',
        self::SORT_HOUSE_COUNT_MORE_TO_LESS        => '有效房源多到少',
        self::SORT_HOUSE_COUNT_LESS_TO_MORE        => '有效房源少到多',
        self::SORT_SITE_CONFIRM_COUNT_MORE_TO_LESS => '确址数量多到少',
        self::SORT_SITE_CONFIRM_COUNT_LESS_TO_MORE => '确址数量少到多',
    ];


    /**
     * 关联点位信息
     * @return HasOne|null
     */
    public function poiRel(): ?HasOne
    {
        switch ($this->wgType) {
            case self::WG_TYPE_MALL:
                return $this->hasOne(SiteLocationMallModel::class, 'wgNumber', 'wgNumber');
            case self::WG_TYPE_STREET:
                return $this->hasOne(MapNationalStreetModel::class, 'wgNumber', 'wgNumber');
            case self::WG_TYPE_UNIVERSITY:
                return $this->hasOne(MapNationalUniversitiesModel::class, 'wgNumber', 'wgNumber');
            case self::WG_TYPE_PARK:
                return $this->hasOne(MapNationalIndustrialParkModel::class, 'wgNumber', 'wgNumber');
            case self::WG_TYPE_TOWN:
                return $this->hasOne(MapTownshipPlanAreaModel::class, 'wgNumber', 'wgNumber');
            default:
                return null;
        }
    }

    /**
     * 关联跟进记录
     * @return HasMany
     */
    public function followUpRel(): HasMany
    {
        return $this->hasMany(CustomerDepartmentFollowUpRecordModel::class, 'taskId', 'id');
    }

    /**
     * 关联线索记录
     * @return HasMany
     */
    public function clueRel(): HasMany
    {
        return $this->hasMany(CustomerDepartmentClueRecordModel::class, 'taskId', 'id')->orderByDesc('followUpTime');
    }

    /**
     * 关联开发经理
     */
    public function userRel(): HasOne
    {
        return $this->hasOne(KmsUser::class, 'rmsUserId', 'assignId');
    }

    /**
     * 关联 KP 客户
     */
    public function kpPartyRel(): HasMany
    {
        return $this->hasMany(CustomerDepartmentKpPartyRecordModel::class, 'taskId', 'id')->orderByDesc('id');
    }


    public function siteLocationMallRel(): HasOne
    {
        return $this->hasOne(SiteLocationMallModel::class, 'wgNumber', 'wgNumber');
    }

}
